<template>
  <div class="loading-container" v-if="visible">
    <!-- 左遮罩 -->
    <div class="mask mask-left" :class="{ 'mask-show': isShowing, 'mask-hide': isHiding }"></div>

    <!-- 右遮罩 -->
    <div class="mask mask-right" :class="{ 'mask-show': isShowing, 'mask-hide': isHiding }"></div>

    <!-- 太极图 -->
    <div class="taiji-container" :class="{ 'taiji-hide': isHiding }">
      <img src="/src/assets/loading.png" alt="Loading" class="taiji-image" />
    </div>
  </div>
</template>

<script>
export default {
  name: "LoadingVue",
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      isShowing: false,
      isHiding: false,
    }
  },
  watch: {
    visible(newVal, oldVal) {
      if (newVal && !oldVal) {
        // 显示动画
        this.showLoading()
      } else if (!newVal && oldVal) {
        // 隐藏动画
        this.hideLoading()
      }
    },
    loading(newVal) {
      // 如果数据加载完成且遮罩已完全显示，开始隐藏动画
      if (!newVal && this.isShowing && !this.isHiding) {
        this.hideLoading()
      }
    },
  },
  mounted() {
    if (this.visible) {
      this.showLoading()
    }
  },
  methods: {
    showLoading() {
      this.isHiding = false
      this.isShowing = true
    },
    hideLoading() {
      this.isHiding = true
      this.isShowing = false

      // 隐藏动画完成后触发事件
      setTimeout(() => {
        this.$emit("animation-complete")
      }, 1000) // 与CSS动画时长保持一致
    },
  },
}
</script>

<style scoped>
.loading-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  pointer-events: auto;
}

/* 遮罩基础样式 */
.mask {
  position: absolute;
  top: 0;
  height: 100%;
  width: 0%;
  background-color: rgba(0, 0, 0, 0.8);
  transition: width 1s cubic-bezier(0.4, 0, 0.2, 1);
}

.mask-left {
  left: 0;
}

.mask-right {
  right: 0;
}

/* 显示动画 */
.mask-show {
  width: 50%;
}

/* 隐藏动画 */
.mask-hide {
  width: 0%;
}

/* 太极图容器 */
.taiji-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10000;
  transition: opacity 0.3s ease;
}

.taiji-image {
  width: 240px;
  height: 240px;
  animation: rotate 2s linear infinite;
}

/* 太极图隐藏动画 */
.taiji-hide {
  opacity: 0;
}

/* 旋转动画 */
@keyframes rotate {
  from {
    transform: rotateY(0deg);
  }
  to {
    transform: rotateY(360deg);
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .taiji-image {
    width: 60px;
    height: 60px;
  }
}

@media (max-width: 480px) {
  .taiji-image {
    width: 50px;
    height: 50px;
  }
}
</style>
